<?php
namespace app\index\domain;

class FormList extends BaseList
{
    var $_needParam = ['limit', 'page'];
    var $_params    = [];
    var $_cache     = [];
	public function __construct($termId)
	{
        $this->_termId = $termId;
        $this->_needParam = array_flip($this->_needParam);
        $this->initParam();
    }

    /**
     * 获取列表中某个内容的数据对象
     */
    protected function createObj($objParam)
    {
        //return new Content($objParam['id']);
        return Classes::get('Form', $objParam['id']);
    }
    
    /**
     * 初始化参数组
     */
    private function initParam()
    {
        if(isset($this->_initParams)){
            $this->_params = $this->_initParams;
            return;
        }
        $this->_initParams = [];
        if(I('get.page'))
            $this->_initParams['page'] = I('get.page');
        $this->_params = $this->_initParams;
        return;
    }

    /**
     * 自定义获取值
     */
    public function valueGet($key)
    {
        if(isset($this->getList()[$key])){
            $index = $this->getList()[$key];
            return $this->createObj($index);
        }
        if($result=$this->setParam($key))
            return $result;
        return null;
    }

    /**
     * 设置参数组
     */
    private function setParam($param)
    {
        if(!strpos($param, "_"))
            return false;
        list($paramKey, $paramValue) = explode('_', $param, 2);
        if(isset($this->_needParam[$paramKey])){
            $this->_params[$paramKey] = $paramValue;
            return $this;
        }
        return false;
    }

    /**
     * 设置limit
     * TODO
     */
    public function getLimit($param)
    {
        if($param == null)
            $this->needParam='limit';
        else
            $this->_params['limit']=$param;
        return $this;
    }

    /**
     * 根据参数组获取列表
     */
    protected function getList()
    {
        $params = $this->_params;
        $cacheKey = md5('FormList'.serialize($params));
        $this->initParam();
        if(isset($this->_cache[$cacheKey]))
            return $this->_cache[$cacheKey];
        if($contentList = D('Form')->getList($this->_termId, $params)){
            return $this->_cache[$cacheKey] = $contentList;
        }
        return null;
    }
}

